package com.citytransport.prokopjuk.importersToDB;

import java.util.ArrayList;
import java.util.Iterator;

import com.citytransport.prokopjuk.JSONparser.JSONParser;
import com.citytransport.prokopjuk.dao.DAOStation;
import com.citytransport.prokopjuk.exception.InvalidFormatFile;
import com.citytransport.prokopjuk.route.Station;

public class JSONImporter {

	private JSONParser parser;
	private  DAOStation crud;
	
	public JSONImporter(String URL)
	{
		parser = new JSONParser(URL);
		crud = new DAOStation();
	}
	
	public void importStation(){
		ArrayList<Station>  list = new ArrayList<Station>(parseStation());
		Iterator<Station> itr = list.iterator();
		while(itr.hasNext()){
			crud.create(itr.next());
		}
		
	}

	private ArrayList<Station> parseStation()
	{
		ArrayList<Station> res = new ArrayList<Station>();
		try
		{
			while(parser.hasNext()){
				parser.next();
			res.add(  new Station(parser.parseInt("stationID"),
					parser.parseString("name"),
					parser.parseBoolean("existingRail"),
					parser.parseBoolean("existingCable")
					)
			);
			}
		} catch (InvalidFormatFile e)
		{
			
			e.printStackTrace();
		}
		return res;
	}	
}
